package com.wdb007.venus.svc.biz.process;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.biz.manager.WSBookShelfEnum;
import com.wdb007.venus.svc.dal.book.dao.BookGridTransDAO;
import com.wdb007.venus.svc.dal.process.dao.ProcessInstructionLogDAO;
import com.wdb007.venus.svc.dal.process.model.ProcessInstructionLogDO;
import com.wdb007.venus.svc.facade.ProcessInstructionService;
import com.wdb007.venus.svc.facade.constant.BizConstants;
import com.wdb007.venus.svc.facade.process.model.ProcessInstructionLogDTO;

@Service("processInstructionService")
public class ProcessInstructionServiceImpl implements ProcessInstructionService {

	private static final Logger logger = LoggerFactory.getLogger(ProcessInstructionServiceImpl.class);
	
	@Autowired 
	private ProcessInstructionLogDAO processInstructionLogDAO;
	
	@Autowired
	private BookGridTransDAO bookGridTransDAO;
	
	@Override
	public Long unlockReqFromAppServer(ProcessInstructionLogDTO pi) {
		
		logger.info("unlockReq========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		//1.获取最大的指令分组流水号
		Long max = processInstructionLogDAO.getMaxGroupId();
		if(null == max) {
			max = 0L;
		}
		Long userid = bookGridTransDAO.getLastestUserByGridCode(pi.getBookgridCode()).getUserId();
		Long groupId = max + 1; 
		pilDO.setInstructionGroupId(groupId);
		pilDO.setUserId(userid);
		pilDO.setInstructionId(1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_unlock_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_APP_SERVER);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return groupId;
	}

	@Override
	public Long unlockRes(ProcessInstructionLogDTO pi) {
		logger.info("unlockRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_unlock_res.name());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setDoorStatus(pi.getDoorStatus());
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long statusReq(ProcessInstructionLogDTO pi) {
		logger.info("statusReq========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_status_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}
	@Override
	public Long statusReqFromAppServer(ProcessInstructionLogDTO pi) {
		logger.info("statusReqFromAppServer========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_status_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_APP_SERVER);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}
	@Override
	public Long statusRes(ProcessInstructionLogDTO pi) {
		logger.info("statusRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_status_res.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setDoorStatus(pi.getDoorStatus());
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long checkReqFromAppServer(ProcessInstructionLogDTO pi) {
		logger.info("checkReqFromAppServer========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		Long groupId = pi.getInstructionGroupId();
		if(null == groupId) {
			groupId = processInstructionLogDAO.getMaxGroupIdByGridCode(pi.getBookgridCode())+1;
		}
		pilDO.setInstructionGroupId(groupId);
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(groupId);
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_check_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_APP_SERVER);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long notifyToAPP(ProcessInstructionLogDTO pi) {
		logger.info("notifyToAPP========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		Long userId = pi.getUserId();
		Long groupId = processInstructionLogDAO.getMaxGroupIdByUserId(userId);
		pilDO.setInstructionGroupId(groupId);
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(groupId);
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(pi.getAction());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setUserId(pi.getUserId());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_APP);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_APP);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		
		return null;
	}

	@Override
	public Long checkRes(ProcessInstructionLogDTO pi) {
		logger.info("bookGridCheckRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookgrid_check_res.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setStockCount(pi.getStockCount());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long bookCaseCheckReq(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseCheckReq========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		//1.获取最大的指令分组流水号
		Long max = processInstructionLogDAO.getMaxGroupId();
		if(null == max) {
			max = 0L;
		}
		Long groupId = max + 1; 
		pilDO.setInstructionGroupId(groupId);
		pilDO.setInstructionId(1);
		pilDO.setAction(WSBookShelfEnum.bookcase_check_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return groupId;
	}

	@Override
	public Long bookCaseCheckRes(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseCheckRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookcase_check_res.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setStockCount(pi.getStockCount());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long bookCaseUnlockReq(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseUnlockReq========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		//1.获取最大的指令分组流水号
		Long max = processInstructionLogDAO.getMaxGroupId();
		if(null == max) {
			max = 0L;
		}
		Long groupId = max + 1; 
		pilDO.setInstructionGroupId(groupId);
		pilDO.setInstructionId(1);
		pilDO.setAction(WSBookShelfEnum.bookcase_unlock_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return groupId;
	}

	@Override
	public Long bookCaseUnlockRes(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseUnlockRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookcase_unlock_res.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

	@Override
	public Long bookCaseStatusReq(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseStatusReq========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		//1.获取最大的指令分组流水号
		Long max = processInstructionLogDAO.getMaxGroupId();
		if(null == max) {
			max = 0L;
		}
		Long groupId = max + 1; 
		pilDO.setInstructionGroupId(groupId);
		pilDO.setInstructionId(1);
		pilDO.setAction(WSBookShelfEnum.bookcase_status_req.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_SEND);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return groupId;
	}

	@Override
	public Long bookCaseStatusRes(ProcessInstructionLogDTO pi) {
		logger.info("bookCaseStatusRes========== {}", JSON.toJSONString(pi));
		ProcessInstructionLogDO pilDO = new ProcessInstructionLogDO();
		pilDO.setInstructionGroupId(pi.getInstructionGroupId());
		Integer maxInstructionId = processInstructionLogDAO.getLastestinstructionId(pi.getInstructionGroupId());
		pilDO.setInstructionId(maxInstructionId+1);
		pilDO.setAction(WSBookShelfEnum.bookcase_status_res.name());
		pilDO.setBookgridCode(pi.getBookgridCode());
		pilDO.setSender(BizConstants.PROCESS_INSTRUCTION_SUBJECT_BOOKCASE);
		pilDO.setRecipient(BizConstants.PROCESS_INSTRUCTION_SUBJECT_WSS_BOOKCASE);
		pilDO.setDirection(BizConstants.PROCESS_INSTRUCTION_DIRECT_RECV);
		pilDO.setContent(pi.getContent());
		pilDO.setOperTime(new Date());
		processInstructionLogDAO.save(pilDO);
		return pi.getInstructionGroupId();
	}

}
